﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblJobsDAO
    {
        DBService _db = null;
        public TblJobsDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable InsertUpdJob(TblJobs Jobs, int mode)
        {
            DataTable flag = null;
            try
            {
                flag = _db.ExeSP_DataTable("sp_TblJobs_InsUpd",
                    new string[] {"@JobID", "@CustomerIndex", "@DateBook", "@JobType", "@CurrentStatus", "@PhoneID", "@ExpectedCompletionDate", "@DateCompleted", 
							      "@RepairCharge", "@LPCharge", "@OtherCharge", "@SalesAmount", "@PaymentMethod", "@QuotationStatus", "@ApprovalStatus", "@FaultDescription", "@OrtheInstructions", "@AccessoriesWithPhone", "@BookByID", "@mode" },
                    new object[] {Jobs.JobID, Jobs.CustomerIndex, Jobs.DateBook, Jobs.JobType, Jobs.CurrentStatus, Jobs.PhoneID, Jobs.ExpectedCompletionDate, Jobs.DateComplete,
                                  Jobs.RepairCharge, Jobs.LPCharge, Jobs.OrtherCharge, Jobs.SalesAmount, Jobs.PaymentMethod, Jobs.QuotationStatus, Jobs.ApprovalStatus, Jobs.FaultDescription, Jobs.OrtheInstructions, Jobs.AccessoriesWithPhone, Jobs.BookByID, mode},
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool UpdateQuoted(int Quoted, int JobID)
        {
            bool flag = false;
            _db.ExeSP_NonQuery("update TblJobs set QuotationStatus = @QuotationStatus where JobID=@JobID",
                new string[] { "QuotationStatus", "@JobID" },
                new object[] { Quoted, JobID },
                CommandType.Text);
            return flag;
        }

        public DataTable Update(int Status, int JobID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("Update TblJobs set CurrentStatus = @Status where JobID = @JobID",
                    new string[] { "@Status", "@JobID" },
                    new object[] {Status, JobID }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetJob(int JobID, string Email)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_JobList",
                    new string[] { "@JobID", "@Email" },
                    new object[] { JobID, Email }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

      
         public DataTable GetJobByCustomerID(int CustomerID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetJobByCustomerID",
                    new string[] { "@CustomerID" },
                    new object[] { CustomerID }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
      
        public DataTable GetJobNonWarranty(int Mode, int type)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetJob",
                    new string[] { "@mode", "@type" },
                    new object[] { Mode, type }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetJobWarranty(int Mode)
        {
            DataTable dt = null;
            try {
                dt = _db.ExeSP_DataTable("sp_GetJobWarranty",
                    new string[] { "@mode" },
                    new object[] { Mode },
                    CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable reportJob1(int JobID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_reportJob",
                    new string[] { "@JobID" },
                    new object[] { JobID }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public List<JobRpt> ReportJob(int JobID)
        {
            List<JobRpt> ListJob = new List<JobRpt>();
            try
            {
               DataTable dt = _db.ExeSP_DataTable("sp_reportJob",
                    new string[] { "@JobID" },
                    new object[] { JobID }, CommandType.StoredProcedure);
               foreach (DataRow r in dt.Rows)
               {
                   JobRpt job = new JobRpt();
                   job.JobID = Convert.ToInt32(r["JobID"]);

                   ListJob.Add(job);
               }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ListJob;
        }

        public DataTable LoadBrandPhone()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblBrandPhone ", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // get job notes
        public DataTable LoadJobNote_ID(int JobID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top(1) * from TblJobStatus where JobID = @JobID order by ID DESC", 
                    new string[]{"@JobID"},
                    new object[]{JobID}, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
